﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AcctrueWMS.Foundation.Web.Modules;
using System.Text.RegularExpressions;
namespace AcctrueWMS.Foundation.Web.Permission
{
    public class UserPermission
    {
        public string ModuleName
        {
            get;
            set;
        }
        public int ActionMask
        {
            get;
            set;
        }
    }
    public class PermissionManager
    {
        /// <summary>
        /// 检查此角色列表 对此url里的某个 controlID 有权限
        /// </summary>
        /// <param name="url"></param>
        /// <param name="controlID"></param>
        /// <param name="roleCodes"></param>
        /// <returns></returns>
        public bool HasPermission(string url, string controlID, string roleCodes, ModuleInfo module)
        {
            #region oldcode

            //ModuleInfo module = GetModule(url);
            //int actionMask = FindAction(module.ModuleName, PermissionSet);
            //if (actionMask == -1)
            //    return false;
            //else
            //{
            //    ModuleAction action = FindActionMask(controlID, module.Actions);
            //    if (action == null)
            //        return false;
            //    else
            //    {
            //        if ((actionMask & (1 << action.MaskNo)) > 0)
            //            return true;
            //        else
            //            return false;
            //    }
            //}
            #endregion

            bool result = false;
            
            if (module == null)
            {
                return false;
            }
            else
            {
                string[] roleCodelist = roleCodes.Split(',');
                int permissionKey = 0;

                //查出角色的权限并集
                foreach (string roleCode in roleCodelist)
                {
                    ModuleAuthorityManager moduleAuthorityManager = Runtime.ModuleAuthorityManager;
                    List<RoleModule> roleList = moduleAuthorityManager.GetAuthorityMaskCode(roleCode);

                    //List<RoleModule> roleList = Runtime.ModuleAuthorityManager.GetAuthorityMaskCode(roleCode);
                    
                    for (int i = 0; i < roleList.Count; i++)
                    {
                        if (roleList[i].ModuleWBS == module.WBSCode)
                        {
                            permissionKey = permissionKey | roleList[i].PrivilegeMask;
                        }
                    }
                }

                //检查此权限位是否有权限
                if(module.Childs[controlID]!=null)
                {
                    int x = permissionKey >> module.Childs[controlID].MaskNo - 1;
                    string s = Convert.ToString(x, 2);
                    string s1 = s.Substring(s.Length - 1, 1);

                    if (s1 == "1")
                    {
                        result = true;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 检查此url是否默认就有权限
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string HasPermission(string url, ModuleInfo module)
        {
            if (module != null)
            {
                if (module.DefaultPrivilege != null)
                    return module.DefaultPrivilege;
                else
                    return "none";
            }
            else
                return "none";

        }
       

        private ModuleAction FindActionMask(string controlID, System.Collections.ObjectModel.Collection<ModuleAction> collection)
        {
            for (int i = 0; i < collection.Count; i++)
            {
                if (collection[i].ControlID == controlID)
                    return collection[i];
                else
                    continue;
            }
            return null;
        }

        private int FindAction(string moduleName, ICollection<UserPermission> PermissionSet)
        {
            if (PermissionSet == null) return -1;
            else
            {
                for (int i = 0; i < PermissionSet.Count; i++)
                {
                    if (moduleName == PermissionSet.ToList<UserPermission>()[i].ModuleName)
                        return PermissionSet.ToList<UserPermission>()[i].ActionMask;
                    else continue;
                }
            }
            return -1;
        }

        private string GetModule(string url)
        {
            string modelName = string.Empty;
            Regex re = new Regex(".*(model=(?<model>.*?)&).*");
            if (re.IsMatch(url))
            {
                string s = re.Match(url).Groups["model"].Value;
                return s;
            }
            else return null;

        }

        public ICollection<UserPermission> PermissionSet
        {
            get;
            set;
        }

        public void AddPermission(string moduleName, int actionMask)
        {
            if (PermissionSet == null)
                PermissionSet = new System.Collections.ObjectModel.Collection<UserPermission>();
            bool foundedPermission = false;
            for (int i = 0; i < PermissionSet.Count; i++)
            {
                if (PermissionSet.ToList<UserPermission>()[i].ActionMask == actionMask && PermissionSet.ToList<UserPermission>()[i].ModuleName == moduleName)
                {
                    foundedPermission = true;
                }
                else
                {
                    continue;
                }
            }
            if (!foundedPermission)
                PermissionSet.Add(new UserPermission() { ModuleName = moduleName, ActionMask = actionMask });


        }
    }
}
